<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.name" placeholder="人员名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="selectemps">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="emps" highlight-current-row v-loading="listLoading"
              @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" label="序号" width="60">
      </el-table-column>
      <el-table-column prop="username" label="姓名" width="150">
      </el-table-column>
      <el-table-column prop="phone" label="电话" width="200">
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="200">
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="100">
      </el-table-column>
      <el-table-column label="状态" width="100" sortable>
        <template slot-scope="scope">
          <span style="color: green" v-if="scope.row.state==1">启用</span>
          <span style="color: red" v-else>禁用</span>
        </template>
      </el-table-column>
      <el-table-column prop="deptName" label="部门" width="150" e>
      </el-table-column>
      <el-table-column prop="logininfoId" label="登陆人" width="150" e>
      </el-table-column>
      <el-table-column prop="shopName" label="店铺" width="150">
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :total="totals"
                     :current-page="currentPage"
                     style="float:right;">
      </el-pagination>
    </el-col>
    <!--新增界面-->
    <!-- 添加或修改的对话框
    el-dialog - elementUi提供的对话框 = 模态框
      :title="title" - 动态改变标题
      :visible.sync="saveFormVisible"  - 模态框的显示状态
      :close-on-click-modal="false" - 关闭模态框只能通过模态框的x关闭，点击空白处不能关闭模态框
    el-form - 表单
      :model="saveForm" - 绑定表单对象
      label-width="80px" - 标签宽度
      :rules="saveFormRules" - 验证规则
      ref="saveForm" - 为当前模态框取名
      prop="name" - 如果当前字段要验证，就必须写，出现*
      auto-complete="off" - 不自动提示
-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="名称" prop="username">
          <el-input v-model="saveForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="saveForm.phone" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="saveForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="saveForm.age" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
              v-model="saveForm.state"
              active-color="#13ce66"
              inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
          <el-form-item label="所属部门">
            <el-select v-model="saveForm.departmentId" clearable placeholder="请选择">
              <el-option
                  v-for="item in depts"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属店铺">
            <el-select v-model="saveForm.shopId" clearable placeholder="请选择">
              <el-option
                  v-for="item in shops"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'
import {getUserListPage, removeUser, batchRemoveUser, editUser, addUser} from '../../api/api';

export default {
  data() {
    return {
      filters: {
        name: ''
      },
      emps: [],
      depts:[],
      shops:[],
      options: [],
      totals: 0,
      currentPage: 1,
      pageSize: 5,
      title: '',
      listLoading: false,
      sels: [],//列表选中列

      saveFormVisible: false,//编辑界面是否显示
      saveLoading: false,
      saveFormRules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ]
      },
      //编辑界面数据
      saveForm: {
        username: '',
        phone: '',
        email: '',
        age: '',
        state: '',
        departmentId: '',
        shopId: '',
      },
    }
  },
  methods: {
    selectemps() {
      this.currentPage = 1;
      this.getEmps();
    },
    //性别显示转换
    formatSex: function (row, column) {
      return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getEmps();
    },
    getShops(){
      this.$http.get("/shop").then((res) => {
        if(res.data.success){
          this.shops = res.data.resultObj;
          this.listLoading = false;
        }
      }).catch((res=>{
        this.$message.error("网络繁忙");
      }));
    },
    //获取用户列表
    getDepts(){
      this.$http.get("/dept").then((res) => {
        if(res.data.success){
          this.depts = res.data.resultObj;
          this.listLoading = false;
        }
      }).catch((res=>{
        this.$message.error("网络繁忙");
      }));
    },
    //获取人员列表
    getEmps() {
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.filters.name
      };
      this.listLoading = true;
      this.$http.post("/emp/pageList", para).then((res) => {
        if (res.data.success) {
          this.emps = res.data.resultObj.data;
          this.totals = res.data.resultObj.totals;
          this.listLoading = false;
        }
      }).catch((res => {
        this.$message.error("网络繁忙");
      }));
    },
    //删除
    handleDel: function (row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/emp/" + row.id).then((res) => {
          this.listLoading = false;
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getEmps();
        });
      }).catch(() => {

      });
    },
    //显示编辑界面
    handleEdit: function (row) {
      this.saveFormVisible = true;
      this.title = '编辑';
      this.saveForm = Object.assign({}, row);
      console.log(this.saveForm)
    },
    //显示新增界面
    handleAdd: function () {
      this.saveFormVisible = true;
      this.title = '新增';
      this.saveForm = {
        username: '',
        phone: '',
        email: '',
        age: '',
        state: false,
        departmentId: '',
        shopId: '',
      };
    },
    //新增
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading = true;
            let para = Object.assign({}, this.saveForm);
            //状态赋值
            para.state = para.state ? 1 : 0
            this.$http.post("/emp", para).then((res) => {
              if (res.data.success) {
                this.saveLoading = false;
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
                this.$refs['saveForm'].resetFields();
                this.saveFormVisible = false;
                this.getEmps();
              }
            }).catch(res => {
              this.$message.error("请求失败");
            });
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        console.log(ids)
        //let para = ids[] ;
        this.$http.patch("/emp", ids).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getEmps();
        });
      }).catch(() => {

      });
    }
  },
  mounted() {
    this.getEmps();
    this.getDepts();
    this.getShops();
  }
}

</script>

<style scoped>

</style>